﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;

namespace CH_k
{
    [Serializable]
    public class ELine : Element
    {

        public Point[] p;

        public Element inEl;
        public Element outEl;
        public int inp_i;
        [NonSerialized()]
        static public Pen stPen;
        static public bool rofrun = false;

        static int lineB = 2;

        public ELine()
        {
            inEl = null;
            outEl = null;
            ElType = "ELine";
        }
        public override void Delete()
        {
            for (int i = 0; i < inEl.outElList.Count; ++i )
            {
                if (inEl.outElList[i] == this)
                {
                    inEl.outElList.Remove(inEl.outElList[i]);
                    break;
                }
            }
            for (int i = 0; i < outEl.inElList.Count; ++i)
            {
                if (outEl.inElList[i] == this)
                {
                    outEl.inElList[i] = null;
                    break;
                }
            }
        }
        public void EndAdding()
        {
            if (outEl != null && inEl != null)
            {
                pCalc();
                inEl.outElList.Add(this);
                outEl.inElList[inp_i] = this;
                Element.seList.Add(this);
                Element.newElement = null;
            }
        }

        public override bool TrySetInput(Point _p, Element _e)
        {
            if (inEl == null)
            {
                input_p = _p;
                inEl = _e;
                EndAdding();
                return true;
            }
            return false;
        }
        public override bool TrySetOutput(Point _p, int _i, Element _e)
        {
            if (outEl == null)
            {
                output_p = _p;
                outEl = _e;
                inp_i = _i;
                EndAdding();
                return true;
            }
            return false;
        }
        public override void pCalc()
        {
            if (input_p.X >= output_p.X - 10 && input_p.X <= output_p.X + 40)
            {
                p = new Point[6];
                p[0] = new Point(input_p.X, input_p.Y);
                p[1] = new Point(input_p.X + 10, input_p.Y);
                p[2] = new Point(input_p.X + 10, input_p.Y - 20);
                p[3] = new Point(input_p.X - 70, input_p.Y - 20);
                p[4] = new Point(input_p.X - 70, output_p.Y);
                p[5] = new Point(output_p.X, output_p.Y);
            }
            else if (input_p.X <= output_p.X + 40)
            {
                p = new Point[4];
                p[0] = new Point(input_p.X, input_p.Y);
                p[1] = new Point(input_p.X + 10, input_p.Y);
                p[2] = new Point(input_p.X + 10, output_p.Y);
                p[3] = new Point(output_p.X, output_p.Y);
            }
            else
            {
                p = new Point[6];
                p[0] = new Point(input_p.X, input_p.Y);
                p[1] = new Point(input_p.X + 10, input_p.Y);
                p[2] = new Point(input_p.X + 10, input_p.Y - 20);
                p[3] = new Point(output_p.X - 10, input_p.Y - 20);
                p[4] = new Point(output_p.X - 10, output_p.Y);
                p[5] = new Point(output_p.X, output_p.Y);
            }
        }
        public override bool Run()
        {
            if (inEl != null)
            {
                return oldState = inEl.Run();
            }
            return false;
        }
        public override bool hover(Point MouseCord)
        {
            for (int i = 1; i < p.Length; ++i)
            {
                if ((MouseCord.X >= p[i - 1].X - lineB && MouseCord.X <= p[i].X + lineB) || (MouseCord.X >= p[i].X - lineB && MouseCord.X <= p[i - 1].X + lineB))
                {
                    if ((MouseCord.Y >= p[i - 1].Y - lineB && MouseCord.Y <= p[i].Y + lineB) || (MouseCord.Y >= p[i].Y - lineB && MouseCord.Y <= p[i - 1].Y + lineB))
                        return true;
                }
            }
            return false;
        }
        public override bool ClickLeft(Point MouseCord)
        {
            if (hover(MouseCord))
            {
                selected = true;
                return true;
            }
            else
            {
                selected = false;
                return false;
            }
        }

        public override void Draw(Graphics _g)
        {
            if(stPen == null) stPen = Pens.Blue;
            if (inEl == null && outEl == null)
            {
                _g.DrawEllipse(Pens.Red, new Rectangle(dec.X - 10, dec.Y - 10, 20, 20));
            }
            else if (inEl != null && outEl == null)
            {
                _g.DrawLine(Pens.Red, dec.X, dec.Y, input_p.X, input_p.Y);
                _g.DrawEllipse(Pens.Red, new Rectangle(dec.X - 10, dec.Y - 10, 20, 20));
            }
            else if (inEl == null && outEl != null)
            {
                _g.DrawLine(Pens.Red, dec.X, dec.Y, output_p.X, output_p.Y);
                _g.DrawEllipse(Pens.Red, new Rectangle(dec.X - 10, dec.Y - 10, 20, 20));
            }
            else if (inEl != null && outEl != null)
            {
                Pen tPen;
                if (selected)
                {
                    tPen = new Pen(Color.Red, 3);
                }
                else
                {
                    if (rofrun) tPen = oldState ? Pens.Green : Pens.Red;
                    else tPen = stPen;
                }
                _g.DrawLines(tPen, p);
            }
        }
    }
}
